home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The PC-SIG Library 10
/
The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso
/
PC_SIGCD
/
22
/
4
/
DISK2247.ZIP
/
CBASE101.ZIP
/
BTREE101.ZIP
/
BTCREATE.C
< prev
next >
Wrap
Text File
|
1990-06-20
|
5KB
|
169 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "@(#)btcreate.c 1.4 - 90/06/20" */
#include <blkio.h>
#include <errno.h>
/*#include <stddef.h>*/
/*#include <string.h>*/
/* local headers */
#include "btree_.h"
/*man---------------------------------------------------------------------------
NAME
btcreate - create a btree
SYNOPSIS
int btcreate(filename, m, keysize, fldc, fldv)
const char *filename;
int m;
size_t keysize;
int fldc;
const btfield_t fldv[];
DESCRIPTION
The btcreate function creates the btree file named by filename.
m is the degree of the btree and keysize is the size of the keys.
fldc is the field count. It specifies the number of fields in
the keys stored in this btree. fldv is an array of field
definition structures. fldv must have fldc elements. The field
definition structure is defined in <btree.h> as type btfield_t.
It has the following members.
size_t offset; /* offset of field in key *\/
size_t len; /* field length *\/
int (*cmp)(void *p1, void *p2, size_t n);
/* comparison function for field *\/
int flags; /* flags *\/
offset and len specify the location and length of the field,
respectively. cmp is a pointer to the user written comparison
function which defines the sort sequence for the field; p1 and p2
point to the two fields to be compared and n is the field length.
The return value must be less than, equal to, or greater than
zero if p1 is less than, equal to, or greater than p2,
respectively. btree field flags values are constructed by
bitwise OR-ing together flags from the following list (one and
only one of the first two may be used).
BT_FASC ascending order
BT_FDSC descending order
The fields in the field definition list must be in order,
starting with the first field in the key.
btopen will fail if one or more of the following is true:
[EEXIST] The named btree file exists.
[EINVAL] filename is the NULL pointer.
[EINVAL] m is less than 3.
[EINVAL] keysize is less than 1.
[EINVAL] fldc is less than 1.
[EINVAL] fldv is the NULL pointer.
[EINVAL] fldv contains an invalid field definition.
[BTEMFILE] Too many open btrees. The maximum is defined as
BTOPEN_MAX in btree.h.
SEE ALSO
btopen.
DIAGNOSTICS
Upon successful completion, a value of 0 is returned. Otherwise,
a value of -1 is returned, and errno set to indicate the error.
------------------------------------------------------------------------------*/
int btcreate(filename, m, keysize, fldc, fldv)
const char *filename;
int m;
size_t keysize;
int fldc;
const btfield_t fldv[];
{
btree_t * btp = NULL;
int terrno = 0; /* tmp errno */
/* validate arguments */
if (filename == NULL || m < 3 || !bt_fvalid(keysize, fldc, fldv)) {
errno = EINVAL;
return -1;
}
/* find free slot in btb table */
for (btp = btb; btp < (btb + BTOPEN_MAX); ++btp) {
if (!(btp->flags & BTOPEN)) {
break; /* found */
}
}
if (btp >= btb + BTOPEN_MAX) {
errno = BTEMFILE; /* no free slots */
return -1;
}
/* load btree_t structure */
btp->bthdr.flh = NIL;
btp->bthdr.m = m;
btp->bthdr.keysize = keysize;
btp->bthdr.flags = 0;
btp->bthdr.root = NIL;
btp->bthdr.first = NIL;
btp->bthdr.last = NIL;
btp->bthdr.keycnt = 0;
btp->bthdr.height = 0;
btp->bp = NULL;
btp->flags = BTREAD | BTWRITE;
btp->fldc = fldc; /* fields */
btp->fldv = NULL;
btp->cbtpos.node = NIL; /* cursor */
btp->cbtpos.key = 0;
btp->cbtnp = NULL;
btp->sp = NULL;
if (bt_alloc(btp) == -1) {
BTEPRINT;
terrno = errno;
memset(btp, 0, sizeof(*btb));
btp->flags = 0;
errno = terrno;
return -1;
}
memcpy(btp->fldv, fldv, btp->fldc * sizeof(*btp->fldv));
/* create file */
btp->bp = bopen(filename, "c", sizeof(bthdr_t), (size_t)1, (size_t)0);
if (btp->bp == NULL) {
if (errno != EEXIST) BTEPRINT;
terrno = errno;
bt_free(btp);
memset(btp, 0, sizeof(*btb));
btp->flags = 0;
errno = terrno;
return -1;
}
/* write header to file */
if (bputh(btp->bp, &btp->bthdr) == -1) { /* header */
BTEPRINT;
terrno = errno;
bclose(btp->bp);
bt_free(btp);
memset(btp, 0, sizeof(*btb));
btp->flags = 0;
errno = terrno;
return -1;
}
/* close btp */
if (btclose(btp) == -1) {
BTEPRINT;
terrno = errno;
bt_free(btp);
errno = terrno;
return -1;
}
errno = 0;
return 0;
}